<?php
/**
 * Created by PhpStorm.
 * User: JieHuiLong <jiehuilong@icloud.com>
 * Date: 2017/3/20
 * Time: 21:18
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Domainame extends Model
{


    public function checkUpdate($ids){
        $this->whereIn('id',$ids)->update(['updated_atwx'=>date('Y-m-d H:i:s',time())]);
    }

    //$status =  [0, 1] => GetQQ域名列表 ; $status =  [-1,1] => GetWechat域名列表 ;
    public function getDomainAll($type = "wechat",$status =  [-1,1])
    {
        if($type === "wechat"){
            $res = Domainame::whereIn('status',$status)
                ->where('updated_atwx','<',date('Y-m-d H:i:s',time()-600))
                ->orWhere('updated_atwx',null)
                ->orderBy('uid', 'desc')->orderBy('updated_atwx', 'desc')->get();
        }else{
            $res = Domainame::whereIn('status',$status)
                ->where('updated_at','<',date('Y-m-d H:i:s',time()-600))
                ->orWhere('updated_at',null)
                ->orderBy('uid', 'desc')->orderBy('updated_at', 'desc')->get();
        }
        return $res;
    }

    static function getQqLists($status = [1,0])
    {
        $uid = auth()->user()->id;
        if ($uid === 1) {
            $data = Domainame::whereIn('status', $status)->select('id', 'uid', 'username', 'domain')->get();
        } else {
            $data = Domainame::where('uid', $uid)->whereIn('status', $status)->select('id', 'uid', 'username', 'domain')->get();
        }
        return $data;
    }

    //QQLists
    static function checkList($status = [1,0],$row = 5)
    {
        $uid = auth()->user()->id;
        $data = array();
        if ($uid === 1) {
            if(Domainame::whereIn('status', $status)->where('uid','>',0)->count() === 0){
                return false;
            }
            $data = Domainame::whereIn('status', $status)->where('ischeckqq',0)->where('check_type','<>',1)->limit($row)->lists('domain','id');
            if(count($data) === 0){
                Domainame::where('ischeckqq',1)->update(['ischeckqq'=>0]);
            }
        } else {
            if(Domainame::whereIn('status', $status)->where('uid', $uid)->count() === 0){
                return false;
            }
            $data = Domainame::whereIn('status', $status)->where('uid', $uid)->where('ischeckqq',0)->where('check_type','<>',1)->limit($row)->lists('domain','id');
            if(count($data) === 0){
                Domainame::where('uid', $uid)->where('ischeckqq',1)->update(['ischeckqq'=>0]);
            }
        }
        return $data;
    }

    static function checkQqSingle($status = [1,0],$row = 1)
    {
        if(Domainame::whereIn('status', $status)->where('uid','>',0)->count() === 0){
                return false;
            }
            $data = Domainame::whereIn('status', $status)->where('ischeckqq',0)->where('check_type','<>',1)->limit($row)->get();
            if(count($data) === 0){
                Domainame::where('ischeckqq',1)->update(['ischeckqq'=>0]);
            }
        return $data;
    }



    //$status =  [-1, -2] => GetQQ域名列表 ; $status =  [1, -1] => GetWechat域名列表 ;
    public function getDomainList($status = [1, -1],$row = 1)
    {
        $uid = auth()->user()->id;
        
        if ($uid === 1) {
            $data = Domainame::whereIn('status', $status)->where('ischeck', 0)->where('check_type','<>', 0)->select('id', 'uid', "domain", "status")
                ->orderBy('updated_atwx','asc')->limit($row)->get();
        } else {
            $data = Domainame::whereIn('status', $status)->where('uid', $uid)->where('ischeck', 0)
                ->where('check_type','<>', 0)->select('id', 'uid', "domain", "status")->orderBy('updated_atwx','asc')->limit($row)->get();
        }
        
        return $data;
    }

    //$status =  [-1, -2] => GetQQ屏蔽列表 ; $status =  [0,-2] => GetWechat屏蔽列表 ;
    public function getBlackList($status =  [-1, -2])
    {
        $uid = auth()->user()->id;
        if ($uid === 1) {
            $res = Domainame::whereIn('status',$status)->select('id', 'username', "domain")->orderBy('uid', 'desc')->orderBy('updated_at', 'desc')->get();
        } else {
            $res = Domainame::where('uid', $uid)->whereIn('status',$status)->select('id', 'username', "domain")->orderBy('updated_at', 'desc')->get();
        }
        return $res;
    }

}
